<?php

namespace App\Admin\Controllers;

use App\Models\OrderSettlement;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class OrderSettlementController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('结算单')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('结算单')
            ->description('信息')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new OrderSettlement);
        $grid->model()->with(['organization', 'organization.type'])->orderBy('id', 'desc');

        $grid->id('Id');
        $grid->column('合作商类型')->display(function () {
            return $this->organization->type->name;
        });
        $grid->column('合作商名称')->display(function () {
            return $this->organization->name;
        });
        $grid->column('start_end_time', '申请时间')->display(function () {
            return sprintf('%s - %s', $this->start_time, $this->end_time);
        });
        $grid->column('order_price', '订单合计(元)')->display(function () {
            return $this->order_price / 100;
        });
        $grid->column('price', '应付合计(元)')->display(function () {
            return $this->price / 100;
        });
        $grid->column('status', '状态')->display(function () {
            return $this->statusText;
        })->label([
            OrderSettlement::STATUS_PROCESSING => 'default',
            OrderSettlement::STATUS_AUDITING => 'info',
            OrderSettlement::STATUS_AUDITED => 'primary',
            OrderSettlement::STATUS_REMIT => 'success',
        ]);
        $grid->column('created_time', '结算时间');

        $grid->actions(function (Grid\Displayers\Actions $actions) {
           $actions->disableEdit();
           $actions->disableDelete();
           $actions->disableView();
           $actions->append(createAction('查看', route('order-settlement-divid.index', ['settlement_id' => $actions->getKey()]), '_blank'));
        });
        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->disableCreateButton();


        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(OrderSettlement::findOrFail($id));

        $show->id('Id');
        $show->org_uid('Org uid');
        $show->org_id('Org id');
        $show->price('Price');
        $show->order_count('Order count');
        $show->start_time('Start time');
        $show->end_time('End time');
        $show->status('Status');
        $show->created_time('Created time');
        $show->updated_time('Updated time');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new OrderSettlement);

        $form->number('org_uid', 'Org uid');
        $form->number('org_id', 'Org id');
        $form->number('price', 'Price');
        $form->number('order_count', 'Order count');
        $form->datetime('start_time', 'Start time')->default(date('Y-m-d H:i:s'));
        $form->datetime('end_time', 'End time')->default(date('Y-m-d H:i:s'));
        $form->number('status', 'Status')->default(1);

        return $form;
    }
}
